package com.frame.platform.post;

import java.util.List;

import com.frame.database.DbOperate;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.platform.user.WfUserPost;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2013
 * @Company:
 * @author 王海锋
 * @CreatedTime:2013-4-11 下午09:53:16
 * @version 1.0
 */

public class PostDao {

    public PageResult query(String name,int size,int pageNo) throws Exception{
        String sql="select * from WFPOST where 1=1";
        Object[] parm=null;
        if(name!=null && !"".equals(name)){
            sql+=" and name like ?";
            parm=new Object[]{"%"+name+"%"};
        }
        sql+=" order by orderid,id";
        return DbOperate.getInstance().query(sql, parm, size, pageNo,PostBean.class);

    }
    /**
     *
     * 功能:获取所有岗位<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2013-4-14 下午10:14:05<br>
     * 说明:<br>
     * 变更:<br>
     */
    public List<PostBean> getList() throws Exception{
        return DbOperate.getInstance().query("select * from WFPOST order by orderid", null,PostBean.class);
    }
    public PostBean getPost(Integer id) throws Exception{
        return (PostBean)DbOperate.getInstance().getBean(PostBean.class, id);
    }
    public void addPost(PostBean bean) throws Exception{
        bean.setOrderId(99999);
        DbOperate.getInstance().saveBean(bean);
    }
    public void delPost(String[] ids) throws Exception{
        if(ids!=null){
            DbOperate db=DbOperate.getInstance();
            for (String id : ids)
                db.delete("delete from WFPOST where id=?", new Object[]{new Integer(id)});

        }
    }
    public void updatePost(PostBean bean) throws Exception{
        DbOperate db=DbOperate.getInstance();
        db.update("update WFPOST set name=? where id=?", new Object[]{bean.getName(),bean.getId()});
    }
    public boolean checkName(String name,Integer id) throws Exception{
        String sql="select id from wfpost where name=?";
        if(id!=null)
            sql+=" and id<>"+id;
        List list=DbOperate.getInstance().query(sql, new Object[]{name});
        if(list.size()>0)
            return false;
        else
            return true;
    }
    public void sortPost(String[] ids) throws Exception{
        if(ids!=null){
            DbOperate db=DbOperate.getInstance();
            for (int i=0;i<ids.length;i++)
                db.delete("update WFPOST set orderid="+i+" where id=?", new Object[]{new Integer(ids[i])});

        }
    }
    public void addUserPost(String userId,String[] postIds) throws Exception{
        DbOperate db=DbOperate.getInstance();
        if(postIds!=null)
            for (String pid : postIds)
                db.update("insert into wfuserpost(userid,postid) values(?,?)", new Object[]{userId,new Integer(pid)});
    }
    public void updateUserPost(String userId,String deptId,String[] postIds) throws Exception{
        DbOperate db=DbOperate.getInstance();
        db.delete("delete from wfuserpost where userid=? and deptId=?", new Object[]{userId,deptId});
        if(postIds!=null)
            for (String pid : postIds)
                db.update("insert into wfuserpost(userid,postid,deptId) values(?,?,?)", new Object[]{userId,new Integer(pid),deptId});
    }
    public List<WfUserPost> getUserPostList(String userId,String deptId) throws Exception{
        String sql="select up.*,p.name postName from wfuserpost up,wfpost p where up.postid=p.id and up.userid=? and up.deptid=? order by p.orderid";
        return DbOperate.getInstance().query(sql, new Object[]{userId,deptId},WfUserPost.class);
    }
}
